package com.xd.pre.modules.tpshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.pre.modules.tpshop.domain.TpCoupon;
import com.xd.pre.modules.tpshop.domain.TpCouponList;
import com.xd.pre.modules.tpshop.mapper.TpCouponListMapper;
import com.xd.pre.modules.tpshop.service.ITpCouponListService;
import com.xd.pre.modules.tpshop.service.ITpCouponService;
import com.xd.pre.modules.tpshop.virtuaBean.CouponVirtual;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@Service
public class TpCouponListServiceImpl extends ServiceImpl<TpCouponListMapper, TpCouponList> implements ITpCouponListService {

    @Autowired
    private ITpCouponService cou_biz;

    //【3】
    @Override //TpCartAction中的findContent_inConformOrder()方法需要调用此方法
    public List<CouponVirtual> findCouponVirtualList_byUid_invoked(int uId) {
        List<TpCouponList> couponList_list = getTpCouponListsByUserId(uId + "");
//        List<TpCouponList> couponList_list = cl_biz.findCouponVirtualList_part1_byUid(uId);
        List<CouponVirtual> cv_list = new ArrayList<CouponVirtual>();
        CouponVirtual cv = null;
        if (couponList_list != null && !couponList_list.isEmpty()) {
            for (int i = 0; i < couponList_list.size(); i++) {
                TpCouponList temp_ = couponList_list.get(i);
                cv = new CouponVirtual(
                        temp_.getCouponlistId() + "",
                        temp_.getType() + "",
                        temp_.getUserId() + "",
                        temp_.getOrderId() + "",
                        temp_.getUseTime() + "",
                        temp_.getCode() + "",
                        temp_.getSendTime() + "",

                        //				( `tp_coupon`.`coupon_id`- 优惠券类型表ID)
                        getTpCouponById(temp_).getUseEndTime() + "",
                        temp_.getCouponId() + "",
                        getTpCouponById(temp_).getName(),
                        getTpCouponById(temp_).getMoney() + "",
                        getTpCouponById(temp_).getCondition() + ""
                );
                cv_list.add(cv);
            }

        }
        return cv_list;
    }

    @Override
    public TpCouponList findCouponList_byCode(String couponCode) {
        LambdaQueryWrapper<TpCouponList> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpCouponList::getCode, couponCode);
        TpCouponList one = getOne(wrapper);
        return one;
    }

    @Override
    public TpCouponList findById(int $coupon_id) {
        return getById($coupon_id);
    }

    @Override
    public int findCouponId_byCouponlist_id(int couponlist_id) {
        LambdaQueryWrapper<TpCouponList> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpCouponList::getCouponlistId, couponlist_id);
        TpCouponList one = getOne(wrapper);
        return one.getCouponId();
    }

    private List<TpCouponList> getTpCouponListsByUserId(String uid) {
        LambdaQueryWrapper<TpCouponList> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCouponList::getUserId, uid);
        return list(lambdaQuery);
    }

    private TpCoupon getTpCouponById(TpCouponList item_) {
        return cou_biz.getById(item_.getCouponId());
    }

}
